<?php

namespace App\Http\Controllers\Home\lei;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Models\Aptitude;
use App\Http\Models\Business;
use Illuminate\Support\Facades\Input;

class AptitudeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = Aptitude::get();
        return view('home.lei.aptitude.index',['data'=>$data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $data = $request -> except('_token');
        if(empty($data['ap_business'])){
            return 2;
        }
        $res = Aptitude::where('ap_business',$data['ap_business'])->first();
        $res1 = Business::where('b_name',$data['ap_business'])->first();
        if($res || $res1){
            return 0;
        }else{
            return 1;
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {   
        
        $data = $request->except('_token');
        $bus = new Business;
        $bus->b_name = $data['ap_business'];
        $bus->b_address = $data['b_address'];
        $bus->b_workTime = $data['startime'].'~~'.$data['endtime'];
        $res = $bus->save();
        $apt = new Aptitude;
        $apt->ap_license = basename($data['license']);
        $apt->ap_permit = basename($data['permit']);
        $apt->ap_phone = $data['b_phone'];
        $apt->ap_name = $data['ap_name'];
        $apt->ap_age = $data['ap_age'];
        $apt->ap_number = $data['ap_number'];
        $apt->ap_business = $data['ap_business'];
        $apt->ap_status = $data['ap_status'];
        $res1 = $apt->save();
        if($res && $res1){
            return view('home.lei.aptitude.success');
        }else{
            return view('home.lei.aptitude.index');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function licenseUpload(Request $request)
    {
//        获取文件对象
        $file = Input::file('file_license');
//        判断文件是否有效
        if($file->isValid()){
            $entension = $file->getClientOriginalExtension();//上传文件的后缀名
            $newName = date('YmdHis').mt_rand(1000,9999).'.'.$entension;
            $path = $file->move(public_path().'/lei/home/images',$newName);
            $filepath = 'lei/home/images/'.$newName;
            return  $filepath;
        }
    }

     public function permitUpload(Request $request)
    {
//        获取文件对象
        $file = Input::file('file_permit');
//        判断文件是否有效
        if($file->isValid()){
            $entension = $file->getClientOriginalExtension();//上传文件的后缀名
            $newName = date('YmdHis').mt_rand(1000,9999).'.'.$entension;
            $path = $file->move(public_path().'/lei/home/images',$newName);
            $filepath = 'lei/home/images/'.$newName;
            return  $filepath;
        }
    }
}
